package com.test.common.component;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.*;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Slf4j
@Component
public class RedisComponent {

	public final static long DEFAULT_EXPIRE = 60 * 60 * 24;
	public final static long NOT_EXPIRE = -1;

	@Resource
	private RedisTemplate<String, Object> redisTemplate;

	@Resource
	private ValueOperations<String, String> valueOperations;

	@Resource
	private HashOperations<String, String, Object> hashOperations;

	@Resource
	private ListOperations<String, Object> listOperations;

	@Resource
	private SetOperations<String, Object> setOperations;

	@Resource
	private ZSetOperations<String, Object> zSetOperations;

	public void set(String key, Object value, long expire) {
		try {
			valueOperations.set(key, toJson(value));
			if (expire != NOT_EXPIRE) {
				redisTemplate.expire(key, expire, TimeUnit.SECONDS);
			}			
		} catch (Exception e) {
			log.warn("Redis set 失败：{} - {} - {} - {}", key, value, expire, e.getMessage());
			e.printStackTrace();
		}
	}

	public void set(String key, Object value) {
		try {
			set(key, value, DEFAULT_EXPIRE);			
		} catch (Exception e) {
			log.warn("Redis set 失败：{} - {} - {}", key, value, e.getMessage());
		}
		
	}

	public <T> T get(String key, Class<T> clazz, long expire) {
		try {
			String value = valueOperations.get(key);
			if (expire != NOT_EXPIRE) {
				redisTemplate.expire(key, expire, TimeUnit.SECONDS);
			}
			return value == null ? null : fromJson(value, clazz);			
		} catch (Exception e) {
			log.warn("Redis get 失败：{} - {} - {}", key, expire, e.getMessage());
			return null;
		}
		
	}

	public <T> T get(String key, Class<T> clazz) {
		try {
			return get(key, clazz, NOT_EXPIRE);			
		} catch (Exception e) {
			log.warn("Redis get 失败：{} - {}", key, e.getMessage());
			return null;
		}
	}

	public String get(String key, long expire) {
		try {
			String value = valueOperations.get(key);
			if (expire != NOT_EXPIRE) {
				redisTemplate.expire(key, expire, TimeUnit.SECONDS);
			}
			return value;
		} catch (Exception e) {
			log.info("Redis get 失败：{} - {} - {}", key, expire, e.getMessage());
			return null;
		}
	}

	public String get(String key) {
		try {
			return get(key, NOT_EXPIRE);	
		} catch (Exception e) {
			log.info("Redis get 失败：{} - {}", key, e.getMessage());
			return null;
		}
	}

	public void delete(String key) {
		try {
			redisTemplate.delete(key);	
		} catch (Exception e) {
			log.info("Redis delete 失败：{} - {}", key, e.getMessage());
		}
		
	}

	public List<Object> getListAll(String key) {
		return listOperations.range(key, 0, listOperations.size(key));
	}

	private String toJson(Object object) {
		if (object instanceof Integer || object instanceof Long || object instanceof Float || object instanceof Double
				|| object instanceof Boolean || object instanceof String) {
			return String.valueOf(object);
		}
		return JSON.toJSONString(object);
	}

	private <T> T fromJson(String json, Class<T> clazz) {
		return JSON.parseObject(json, clazz);
	}

}
